<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js webgl - modifier - Subdivisions using Loop Subdivision Scheme</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<style>
			body {
				font-family: Monospace;
				background-color: #f0f0f0;
				margin: 0px;
				overflow: hidden;
			}
		</style>
	</head>
	<body>

		<script src="../build/three.js"></script>
		<script src="js/controls/OrbitControls.js"></script>
		<script src="js/modifiers/SimplifyModifier.js"></script>
		<script src="js/libs/stats.min.js"></script>

		<script>

			var container, stats;

			var camera, controls, scene, renderer;

			var cube, mesh, material;

			var boxGeometry = new THREE.BoxGeometry( 80, 80, 80, 4, 4, 4 );
			var torusGeometry = new THREE.TorusGeometry( 50, 25, 8, 8, Math.PI * 2 );
			var sphereGeometry = new THREE.SphereGeometry( 50, 15, 15 );
			var planeGeometry = new THREE.PlaneGeometry( 100, 100, 6, 6 );
			var textGeometry;

			var loader = new THREE.FontLoader();
			loader.load( 'fonts/helvetiker_regular.typeface.json', function ( font ) {

					textGeometry = new THREE.TextGeometry( '&', {
					size: 200,
					height: 50,
					curveSegments: 1,
					font: font
				} );

			} );

			var modifer = new THREE.SimplifyModifier();
			var meshes = [];
			var count = 0;


			init();
			animate();


			function addStuff( geometry ) {
				count ++;

				var simplified = modifer.modify( geometry, geometry.vertices.length * 0.5 | 0 );

				var wireframe = new THREE.MeshBasicMaterial({
					color: Math.random() * 0xffffff,
					wireframe: true
				});

				var materialNormal = new THREE.MeshNormalMaterial({
					transparent: true,
					opacity: 0.7

				});

				mesh = THREE.SceneUtils.createMultiMaterialObject( simplified, [
					material,
					wireframe,
					// materialNormal
				]);

				mesh.position.x = -200;
				mesh.position.y = count * 150 - 300;

				scene.add( mesh );
				meshes.push( mesh );


				mesh = THREE.SceneUtils.createMultiMaterialObject( geometry, [
					material,
					wireframe,
					// materialNormal
				]);

				mesh.position.x = 200;
				mesh.position.y = count * 150 - 300;
				scene.add( mesh );
				meshes.push ( mesh );

			}

			function init() {

				container = document.createElement( 'div' );
				document.body.appendChild( container );

				info = document.createElement( 'div' );
				info.style.position = 'absolute';
				info.style.top = '10px';
				info.style.width = '100%';
				info.style.textAlign = 'center';
				info.innerHTML = '50% Vertex Reduction using SimplifyModifier';
				container.appendChild( info );

				camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
				camera.position.z = 500;

				scene = new THREE.Scene();

				var light = new THREE.PointLight( 0xffffff, 1.5 );
				light.position.set( 1000, 1000, 2000 );
				scene.add( light );

				addStuff( planeGeometry );
				addStuff( boxGeometry );
				addStuff( sphereGeometry );
				addStuff( torusGeometry );





				renderer = new THREE.WebGLRenderer( { antialias: true } );
				renderer.setClearColor( 0xf0f0f0 );
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
				container.appendChild( renderer.domElement );

				stats = new Stats();
				container.appendChild( stats.dom );

				//

				controls = new THREE.OrbitControls( camera, renderer.domElement );

				window.addEventListener( 'resize', onWindowResize, false );

			}

			function onWindowResize() {

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

				renderer.setSize( window.innerWidth, window.innerHeight );

			}

			//

			function animate() {

				meshes.forEach( m => {
					m.rotation.x += 0.01;
					m.rotation.y += 0.01;
					m.rotation.z += 0.01;
				})

				requestAnimationFrame( animate );

				controls.update();

				stats.begin();
				render();
				stats.end();

			}

			function render() {

				renderer.render( scene, camera );

			}

		</script>

	</body>
</html>
